package com.me.modules.incident.controller;

import java.util.ArrayList;
import java.util.List;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.controller.BaseController;
import com.me.modules.common.page.Page;
import com.me.modules.common.utils.*;
import com.me.modules.sys.entity.ExtendField;
import com.me.modules.sys.entity.OperationEntity;
import com.me.modules.sys.entity.ExtendFieldValue;
import com.me.modules.sys.service.ExtendFieldService;
import com.me.modules.sys.service.OperationService;
import com.me.modules.sys.service.ExtendFieldValueService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.me.modules.incident.entity.IncidentInfoEntity;
import com.me.modules.incident.service.IncidentInfoService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 事件工单
 *
 * @author jindouyun
 */
@Controller
@RequestMapping("/incident/incidentinfo")
public class IncidentInfoController extends BaseController {
    @Resource
    private IncidentInfoService incidentInfoService;
    @Resource
    private ExtendFieldService extendFieldService;
    @Resource
    private ExtendFieldValueService valueDictionaryService;
    @Resource
    private OperationService operationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("incidentinfo:list")
    @SysLog("查询事件列表")
    public String list(IncidentInfoEntity incidentInfo, Model model, HttpServletRequest request) {
        int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
        Page<IncidentInfoEntity> page = incidentInfoService.findPage(incidentInfo, pageNum);
        List<IncidentInfoEntity> incidentList = page.getResult();
        List<IncidentInfoEntity> newIncidentList = new ArrayList<>();
        List<ExtendField> fieldList = extendFieldService.queryByTableName("incident");
        if (CollectionUtils.isNotEmpty(fieldList) && CollectionUtils.isNotEmpty(incidentList)) {
            for (IncidentInfoEntity incident : incidentList) {
                String tableId = incident.getId().toString();

                List<ExtendFieldValue> valueList = new ArrayList<>();
                for (ExtendField fieldDictionaryEntity : fieldList) {
                    Long fieldID = fieldDictionaryEntity.getId();

                    ExtendFieldValue value = valueDictionaryService.findByFieldIdAndUserId(fieldID, tableId);
                    if (value != null) {
                        valueList.add(value);
                    } else {
                        valueList.add(new ExtendFieldValue());
                    }
                }
                incident.setValueDictionarys(valueList);
                newIncidentList.add(incident);
            }

            model.addAttribute("page", new Page(newIncidentList, page.getTotal(), page.getPageSize(), page.getPageNum()));

        } else {
            model.addAttribute("page", page);

        }
        //model.addAttribute("page",new Page(newleaveList,page.getTotal(),page.getPageSize() , page.getPageNum()));
        model.addAttribute("incidentInfo", incidentInfo);
        model.addAttribute("fieldList", fieldList);
        return "incident/incident";
    }


    /**
     * 信息
     */
    @RequestMapping("info")
    @RequiresPermissions("incidentinfo:info")
    @SysLog("查询事件工单信息")
    public String info(Model model, Long id, HttpServletRequest request) {
        List<ExtendField> fieldList = extendFieldService.queryByTableName("incident");
        if (id != null) {
            IncidentInfoEntity incidentInfo = incidentInfoService.queryObject(id);
            if (fieldList.size() > 0) {
                String tableId = incidentInfo.getId().toString();
                List<ExtendFieldValue> valueList = new ArrayList<ExtendFieldValue>();
                for (int j = 0; j < fieldList.size(); j++) {
                    Long fieldID = fieldList.get(j).getId();

                    ExtendFieldValue value = valueDictionaryService.findByFieldIdAndUserId(fieldID, tableId);
                    if (value != null) {
                        valueList.add(value);
                    } else {
                        valueList.add(new ExtendFieldValue());
                    }
                }
                incidentInfo.setValueDictionarys(valueList);
            }
            model.addAttribute("incidentInfo", incidentInfo);
        } else {
            IncidentInfoEntity incidentInfo = new IncidentInfoEntity();
            incidentInfo.setIncNo(Utils.getSN("INC_", "yyyyMMddHHmmss", 2));
            model.addAttribute("incidentInfo", incidentInfo);
        }

        model.addAttribute("fieldList", fieldList);
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree", JsonUtil.getJsonByObj(operationList));
        return "incident/incidentEdit";
        //IncidentInfoEntity incidentInfo = incidentInfoService.queryObject(id);

        //return Result.ok().put("incidentInfo", incidentInfo);
    }

    /**
     * 编辑
     *
     * @param incidentInfo
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @RequiresPermissions("incidentinfo:save")
    @ResponseBody
    public Result edit(IncidentInfoEntity incidentInfo) {
        if (incidentInfo.getId() == null) {
            incidentInfoService.save(incidentInfo);
            if (incidentInfo.getValueDictionarys().size() > 0) {
                for (ExtendFieldValue value : incidentInfo.getValueDictionarys()) {
                    if (!StringUtils.isEmpty(value.getFieldValue())) {
                        value.setTableId(incidentInfo.getId().toString());
                        valueDictionaryService.save(value);
                    }
                }
            }
        } else {
            incidentInfoService.update(incidentInfo);
            if (incidentInfo.getValueDictionarys().size() > 0) {
                for (ExtendFieldValue value : incidentInfo.getValueDictionarys()) {
                    ExtendFieldValue valueDictionaryEntity = valueDictionaryService.findByFieldIdAndUserId(value.getFieldId(), incidentInfo.getId().toString());
                    if (valueDictionaryEntity != null) {
                        valueDictionaryService.delete(valueDictionaryEntity.getId());
                    }
                    if (!StringUtils.isEmpty(value.getFieldValue())) {
                        value.setTableId(incidentInfo.getId().toString());
                        valueDictionaryService.save(value);
                    }


                }
            }
        }
        return Result.ok();
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("incidentinfo:save")
    @SysLog("保存事件工单")
    public Result save(@RequestBody IncidentInfoEntity incidentInfo) {
        incidentInfoService.save(incidentInfo);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("incidentinfo:update")
    @SysLog("修改事件工单")
    public Result update(@RequestBody IncidentInfoEntity incidentInfo) {
        incidentInfoService.update(incidentInfo);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("incidentinfo:delete")
    @SysLog("删除事件工单")
    @ResponseBody
    public Result edit(Long id) {
        if (incidentInfoService.delete(id) < 1) {
            return Result.error("删除事件工单失败");
        }
        return Result.ok("删除事件工单成功");
    }


}
